﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace FACUI.BasicClass
{


    public static class DeviceInfo
    {
        public static string GetDCDevType(string devName)
        {
            if (string.IsNullOrEmpty(devName))
            {
                return "";
            }
            devName = devName.ToUpper();
            if (devName.Contains("J") || devName.Contains("X"))
            {
                return "提速道岔";
            }
            return "普通道岔";
        }

        public static Dictionary<string, string> GetDevAllNames(StationCfg stacfg, string devName)
        {
            return GetDevAllNames(stacfg, devName, "");
        }

        public static Dictionary<string, string> GetDevAllNames(StationCfg stacfg, string devName, string devType)
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            string str = "设备信息";
            if (devType == "智能分析")
            {
                str = @"智能分析\设备信息";
            }
            CfgNode nodeDirect = stacfg.GetStaCfgRoot().GetNodeDirect(str + @"\" + devName + @"\设备列表");
            if (nodeDirect != null)
            {
                foreach (CfgNode node2 in nodeDirect.GetSubNodeList())
                {
                    string str2 = node2["设备名称"].Trim();
                    string key = node2["ID"];
                    if (!dictionary.ContainsKey(key))
                    {
                        dictionary.Add(key, str2);
                    }
                }
            }
            return dictionary;
        }
        
        public static string GetGDType(string devName)
        {
            if (string.IsNullOrEmpty(devName))
            {
                return "";
            }
            devName = devName.ToUpper();
            if (devName.Contains("DG"))
            {
                return "道岔区段";
            }
            if ((((devName.Contains("WG") || devName.Contains("AG")) || (devName.Contains("BG") || devName.Contains("JG"))) || ((devName[0] == 'D') && devName.EndsWith("G"))) || (devName.Contains("/") && devName.EndsWith("G")))
            {
                return "无岔区段";
            }
            return "股道";
        }

        public static List<string> GetKglNames(StationCfg staCfg)
        {
            List<string> list = new List<string>();
            CfgNode kglCfgNode = staCfg.GetKglCfgNode();
            CfgNode mapInfoCfgNode = staCfg.GetMapInfoCfgNode();
            if (kglCfgNode == null)
            {
                return null;
            }
            CfgNode subNode = kglCfgNode.GetSubNode("实际开关量", true);
            int num = -1;
            foreach (CfgNode node4 in subNode.GetSubNodeList())
            {
                if (!list.Contains(node4["开关量名称"]))
                {
                    list.Add(node4["开关量名称"]);
                }
                if (Util.ToInt(node4["ID"], -1) > num)
                {
                    num = Util.ToInt(node4["ID"], -1);
                }
            }
            if (staCfg.IsCfgExpert)
            {
                subNode = kglCfgNode.GetSubNode("智能分析逻辑开关量", true);
                foreach (CfgNode node4 in subNode.GetSubNodeList())
                {
                    if (!list.Contains(node4["开关量名称"]))
                    {
                        list.Add(node4["开关量名称"]);
                    }
                    if (Util.ToInt(node4["ID"], -1) > num)
                    {
                        num = Util.ToInt(node4["ID"], -1);
                    }
                }
            }
            return list;
        }

        public static int GetMaxID(DataTable datatable)
        {
            int num = -1;
            foreach (DataRow row in datatable.Rows)
            {
                if (((int)row["ID"]) > num)
                {
                    num = (int)row["ID"];
                }
            }
            return num;
        }

        public static int GetMaxID(KglCollection kglList)
        {
            if (kglList == null)
            {
                return -1;
            }
            int id = -1;
            foreach (Kgl kgl in (IEnumerable)kglList)
            {
                if (kgl.id > id)
                {
                    id = kgl.id;
                }
            }
            return id;
        }


        public static string GetXhjType(string XhjName)
        {
            XhjName = XhjName.ToUpper();
            if (string.IsNullOrEmpty(XhjName))
            {
                return "";
            }
            if ((XhjName[0] == 'S') || (XhjName[0] == 'X'))
            {
                if ((XhjName.Length == 1) || (XhjName[1] == 'F'))
                {
                    return "进站信号机";
                }
                return "列兼调信号机";
            }
            if (XhjName[0] == 'D')
            {
                return "普通调车信号机";
            }
            return "区间信号机";
        }

        public static KglCollection GetZctLogicKglList(StationCfg staCfg, int maxID)
        {
            KglCollection kgls = new KglCollection();
            CfgNode mapInfoCfgNode = staCfg.GetMapInfoCfgNode();
            if (mapInfoCfgNode == null)
            {
                return null;
            }
            CfgNode subNode = mapInfoCfgNode.GetSubNode("概述");
            int num = 0;
            if (subNode != null)
            {
                num = Convert.ToInt32(subNode["逻辑开关量数目"]);
            }
            CfgNode node3 = mapInfoCfgNode.GetSubNode("逻辑开关量");
            Logic logic = null;
            for (int i = 1; i <= num; i++)
            {
                logic = MapData.createLogic(node3[i.ToString()]);
                Kgl kgl = new Kgl(logic.name)
                {
                    SubKgl1Name = logic.d1,
                    SubKgl2Name = logic.d2,
                    SubKglOp = logic.oper.ToString(),
                    id = ++maxID
                };
                kgls.Add(kgl);
            }
            return kgls;
        }

        public static void SetDevInfo(CfgNode devList, string devname)
        {
            if ((devList != null) && !string.IsNullOrEmpty(devname))
            {
                foreach (CfgNode node in devList.GetSubNodeList())
                {
                    string str2;
                    if (string.IsNullOrEmpty(node["没有的属性"]))
                    {
                        str2 = "#";
                    }
                    else
                    {
                        str2 = node["没有的属性"];
                    }
                    string devName = node["设备名称"].ToUpper();
                    if ((devname == "电源屏") && string.IsNullOrEmpty(node["天窗类型"]))
                    {
                        node["天窗类型"] = "垂直天窗";
                    }
                    else if (devname == "道岔表示")
                    {
                        if (string.IsNullOrEmpty(node["天窗类型"]))
                        {
                            node["天窗类型"] = "垂直天窗";
                        }
                        if (string.IsNullOrEmpty(node["子类型"]))
                        {
                            node["子类型"] = GetDCDevType(devName);
                        }
                    }
                    else
                    {
                        string[] strArray;
                        if (devname == "信号机")
                        {
                            if (devName == "DUMMY")
                            {
                                continue;
                            }
                            if ((devName[0] == 'S') || (devName[0] == 'X'))
                            {
                                strArray = new string[] { "非进路调车灯#", "JKD#" };
                            }
                            else if (devName[0] == 'D')
                            {
                                strArray = new string[] { "列车信号机1DJ点灯电流#", "列车信号机2DJ点灯电流#", "X-U#", "X-L#" };
                            }
                            else
                            {
                                strArray = new string[] { "X-B#", "ZR#", "ZQ#", "非进路调车灯#", "JKD#" };
                            }
                            foreach (string str4 in strArray)
                            {
                                if (!str2.Contains(str4))
                                {
                                    str2 = str2 + str4;
                                }
                            }
                            if (string.IsNullOrEmpty(node["天窗类型"]))
                            {
                                node["天窗类型"] = "垂直天窗";
                            }
                            if (string.IsNullOrEmpty(node["子类型"]))
                            {
                                node["子类型"] = GetXhjType(devName);
                            }
                        }
                        else if (devname == "轨道")
                        {
                            if (string.IsNullOrEmpty(node["天窗类型"]))
                            {
                                node["天窗类型"] = "垂直天窗";
                            }
                            if (string.IsNullOrEmpty(node["子类型"]))
                            {
                                node["子类型"] = GetGDType(devName);
                            }
                            if (node["子类型"] != "道岔区段")
                            {
                                strArray = new string[] { "区故解#", "区段按钮#", "ZR#", "事故按钮#" };
                                foreach (string str4 in strArray)
                                {
                                    if (!str2.Contains(str4))
                                    {
                                        str2 = str2 + str4;
                                    }
                                }
                            }
                        }
                    }
                    node["没有的属性"] = str2;
                }
            }
        }

        public static void SetPowerAD(CfgNode propNode, StationCfg stacfg)
        {
            foreach (CfgNode node in propNode.GetSubNodeList())
            {
                SetPowerAD(node["ID"], node["AD最大"].Trim(), propNode.NodeName, stacfg);
            }
        }

        public static void SetPowerAD(string devID, string value, string propname, StationCfg stacfg)
        {
            try
            {
                CfgNode node = null;
                CfgNode node2 = null;
                string str = "0";
                if (propname == "电源屏电压")
                {
                    node = stacfg.GetDevPropCfgNode("电源屏", "电源屏电流", false);
                }
                else
                {
                    node = stacfg.GetDevPropCfgNode("电源屏", "电源屏电压", false);
                }
                node2 = stacfg.GetDevPropCfgNode("电源屏", "电源屏功率", false);
                if ((node != null) && (node2 != null))
                {
                    foreach (CfgNode node3 in node.GetSubNodeList())
                    {
                        if (devID == node3["ID"])
                        {
                            str = node3["AD最大"];
                            break;
                        }
                    }
                    foreach (CfgNode node3 in node2.GetSubNodeList())
                    {
                        if (devID == node3["ID"])
                        {
                            node3["AD最大"] = string.Format("{0:0.000}", (Convert.ToSingle(value) * Convert.ToSingle(str)) / 1000f);
                            node3["曲线显示上限"] = string.Format("{0:0.000}", (Convert.ToSingle(value) * Convert.ToSingle(str)) / 1000f);
                            return;
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Log.appendLog(exception.ToString(), "");
            }
        }
    }
}

